package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.*;
import com.jy.wms.service.WmsOwnerSupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsOwnerSupplierController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author JaneLiu
 * @Date Tue Sep 05 16:12:01 CST 2017
 */

@Controller
@RequestMapping("/wmsownersupplier")
@Api(value = "/wmsownersupplier", description = "关于货主供应商的操作")
public class  WmsOwnerSupplierController extends BaseController<WmsOwnerSupplierService,WmsOwnerSupplier> {
    /**
     * 货主供应商列表
     * @param entity
     * @param session
     * @return
     */
    @ApiOperation(value = "货主供应商管理 POST请求查询货主供应商信息",response = JSONObject.class, notes = "POST请求查询货主供应商信息")
    @PostMapping(value="/list")
    public @ResponseBody JSONObject getWmsOwnerSupplier(@RequestBody WmsOwnerSupplier entity,HttpSession session) {
        Page<WmsOwnerSupplier> list= new Page<WmsOwnerSupplier>();
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid=String.valueOf(1077);//仓库编号
//            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            List<SysHz> hz = new ArrayList<>();
            SysHz sysHz = new SysHz();
            sysHz.setHzid("f36c56551c2242819ad601b13e45653f");
            sysHz.setShow_hzid("吴金霏的皮革厂");
            hz.add(sysHz);
            if(hz.size()>0){
                entity.setSshzid(hz.get(0).getHzid());//货主编号
                entity.setSshzname(hz.get(0).getShow_hzid());//货主名称
                entity.setCkid(ckid);
                list= baseService.baseSelectPage(entity);
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 新增保存
     * @param entity
     * @param session
     * @return
     */
    @PostMapping(value = "add")
    @ApiOperation(value = "供应商信息 新增供应商信息",response = JSONObject.class, notes = "POST新增供应商信息")
    public @ResponseBody JSONObject addWmsOwnerSupplier(@RequestBody WmsOwnerSupplier entity,HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            if(hz.size()>0){
                entity.setSshzid(hz.get(0).getHzid());//货主编号
                entity.setSshzname(hz.get(0).getShow_hzid());//货主名称
            }
            WmsOwnerSupplier entitys=baseService.searchADD(entity.getBh(),ckid,entity.getSshzid());
            if(entitys!=null){
                json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.info.toString(),"新增供应商编号重复","新增供应商编号重复","");

            }else {
                entity.setGj("中国");
                entity.setId(UUIDUtils.uuid());
                entity.setCkid(ckid);
                baseService.baseInsert(entity);
            }
        } catch (SucessException e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "新增供应商信息成功", "", JSONObject.fromObject(entity).toString());
        } catch (ServiceException e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "新增供应商信息失败", e.getMessage(), "");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增供应商信息失败","新增供应商信息失败","");
        }
        return json;
    }

    /**
     * 添加前查询当前用户对应货主名称
     * @param session
     * @return
     */
    @PostMapping(value = "addSearch")
    @ApiOperation(value = "供应商信息 查询当前货主名称",response = JSONObject.class, notes = "POST查询货主名称")
    public @ResponseBody JSONObject addSearch(HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            if(hz.size()>0){
                json.put("sshzname",hz.get(0).getShow_hzid());//货主名称
                json.put("sshzid",hz.get(0).getHzid());//货主编号
            }else {
                json.put("sshzname","");//货主名称
                json.put("sshzid","");//货主编号
            }
            return json;
        } catch (Exception e) {
            e.printStackTrace();
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "查询货主名称失败", "查询货主名称失败", "");
            return json;
        }
    }
    /**
     * 根据货主id查询货主所有供应商
     * @param map
     * @return
     */
    @PostMapping(value = "/getWmsOwnerSupplierByHzId")
    @ApiOperation(value = "货主供应商 查看单个货主的所有供应商",response = JSONObject.class, notes = "POST查看单个货主的所有供应商")
    public @ResponseBody JSONObject getWmsOwnerSupplierByHzId(@RequestBody Map<String,Object> map,HttpSession session) {
        JSONObject json = getJSONObject();
        List<WmsOwnerSupplier> entity=  new ArrayList<WmsOwnerSupplier>();

        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            String owner=null;
            if(hz.size()>0){
                owner=hz.get(0).getHzid();//货主名称
            }
            String mc="";
            if(map.containsKey("mc")){
                mc=map.get("mc").toString();
            }
            entity = baseService.baseSelectBySshzid(owner,mc,ckid);
            json =SetStatus.setSearchType(JSONArray.fromObject(entity).toString(),entity.size());
        } catch (Exception e) {
            e.printStackTrace();
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查看供应商信息失败","查看单个货主的所有供应商失败","");
        }
        return json;
    }

    /**
     * 国家查询
     * @param session
     * @return
     */
    @PostMapping(value = "/searchGJ")
    @ApiOperation(value = "国家查询 查看国家信息",response = JSONObject.class, notes = "POST查看国家信息")
    public @ResponseBody JSONObject searchGJ(HttpSession session) {
        JSONObject json = new JSONObject();
        try {
            String id="0";
            WmsAddressDictionary wmsAddressDictionary = baseService.searchGJ(id);
            json.put("gj",wmsAddressDictionary.getId());
            json.put("show_gj",wmsAddressDictionary.getMc());
        } catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查看国家信息失败","查看国家信息失败","");
        }
        return json;
    }
    /**
     * 省查询
     * @param session
     * @return
     */
    @PostMapping(value = "/searchS")
    @ApiOperation(value = "省查询 查看省信息",response = JSONArray.class,notes = "POST查看省输入</br>{\"gj\":\"国家编号\",</br>\"show_sheng\":\"省名称\"}")
    public @ResponseBody JSONArray searchS(@RequestBody Map<String,Object> map,HttpSession session) {
        JSONObject json = new JSONObject();
        JSONArray jsonArray=new JSONArray();
        try {
            List<WmsAddressDictionary> list = baseService.searchS(map.get("gj").toString(),map.get("show_sheng").toString());
            for(WmsAddressDictionary wmsAddressDictionary:list){
                json.put("sheng",wmsAddressDictionary.getId());
                json.put("show_sheng",wmsAddressDictionary.getMc());
                jsonArray.add(json);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }
    /**
     * 城市查询
     * @param session
     * @return
     */
    @PostMapping(value = "/searchCS")
    @ApiOperation(value = "城市查询 查看城市信息",response = JSONArray.class,notes = "POST查询城市输入</br>{\"sheng\":\"省份编号\",</br>\"show_cs\":\"城市名称\"}")
    public @ResponseBody JSONArray searchCS(@RequestBody Map<String,Object> map,HttpSession session) {
        JSONObject json = new JSONObject();
        JSONArray jsonArray=new JSONArray();
        try {
            List<WmsAddressDictionary> list = baseService.searchS(map.get("sheng").toString(),map.get("show_cs").toString());
            for(WmsAddressDictionary wmsAddressDictionary:list){
                json.put("cs",wmsAddressDictionary.getId());
                json.put("show_cs",wmsAddressDictionary.getMc());
                jsonArray.add(json);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }
}
